<template>
  <div class="dict-container">
    <div class="dict-header">
      <div class="search-wrapper">
        <el-form :inline="true" :model="queryParams" class="search-form">
          <el-form-item label="字典名称">
            <el-input 
              v-model="queryParams.name" 
              placeholder="请输入字典名称" 
              clearable
              @keyup.enter="handleSearch"
            >
              <template #prefix>
                <el-icon><Document /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="字典类型">
            <el-input 
              v-model="queryParams.type" 
              placeholder="请输入字典类型" 
              clearable
              @keyup.enter="handleSearch"
            >
              <template #prefix>
                <el-icon><Collection /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select 
              v-model="queryParams.status" 
              placeholder="请选择状态" 
              clearable
              style="width: 120px"
            >
              <el-option label="启用" value="enabled">
                <el-icon color="#67C23A"><CircleCheck /></el-icon>
                <span style="margin-left: 8px">启用</span>
              </el-option>
              <el-option label="禁用" value="disabled">
                <el-icon color="#909399"><CircleClose /></el-icon>
                <span style="margin-left: 8px">禁用</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button-group>
              <el-button type="primary" @click="handleSearch">
                <el-icon><Search /></el-icon>搜索
              </el-button>
              <el-button @click="resetQuery">
                <el-icon><Refresh /></el-icon>重置
              </el-button>
            </el-button-group>
          </el-form-item>
        </el-form>

        <el-button type="primary" @click="handleAdd">
          <el-icon><Plus /></el-icon>新增字典
        </el-button>
      </div>

      <div class="stat-cards">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-card shadow="hover" class="stat-card">
              <template #header>
                <div class="card-header">
                  <span>总字典数</span>
                  <el-tag type="info" effect="plain">全部</el-tag>
                </div>
              </template>
              <div class="card-body">
                <span class="number">{{ stats.total }}</span>
                <span class="label">个</span>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover" class="stat-card">
              <template #header>
                <div class="card-header">
                  <span>启用字典</span>
                  <el-tag type="success" effect="plain">正常</el-tag>
                </div>
              </template>
              <div class="card-body">
                <span class="number">{{ stats.enabled }}</span>
                <span class="label">个</span>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover" class="stat-card">
              <template #header>
                <div class="card-header">
                  <span>禁用字典</span>
                  <el-tag type="info" effect="plain">已禁用</el-tag>
                </div>
              </template>
              <div class="card-body">
                <span class="number">{{ stats.disabled }}</span>
                <span class="label">个</span>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover" class="stat-card">
              <template #header>
                <div class="card-header">
                  <span>今日新增</span>
                  <el-tag type="warning" effect="plain">新增</el-tag>
                </div>
              </template>
              <div class="card-body">
                <span class="number">{{ stats.today }}</span>
                <span class="label">个</span>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>

    <div class="dict-content">
      <el-table :data="dictList" style="width: 100%">
        <el-table-column prop="name" label="字典名称" min-width="180" />
        <el-table-column prop="type" label="字典类型" width="180" />
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="row.status === 'enabled' ? 'success' : 'info'">
              {{ row.status === 'enabled' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="180" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="primary" link @click="handleData(row)">
              数据
            </el-button>
            <el-button type="danger" link @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 字典表单对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="formType === 'add' ? '新增字典' : '编辑字典'"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="type">
          <el-input v-model="formData.type" placeholder="请输入字典类型" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="formData.status"
            active-value="enabled"
            inactive-value="disabled"
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="formData.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 字典数据对话框 -->
    <el-dialog
      v-model="dataDialogVisible"
      :title="`字典数据 - ${currentDict?.name}`"
      width="800px"
      :close-on-click-modal="false"
    >
      <div class="dict-data-header">
        <el-button type="primary" @click="handleAddData">
          <el-icon><Plus /></el-icon>新增
        </el-button>
      </div>
      <el-table :data="dictDataList" style="width: 100%">
        <el-table-column prop="label" label="字典标签" min-width="180" />
        <el-table-column prop="value" label="字典值" width="180" />
        <el-table-column prop="sort" label="排序" width="100" align="center" />
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="row.status === 'enabled' ? 'success' : 'info'">
              {{ row.status === 'enabled' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEditData(row)">
              编辑
            </el-button>
            <el-button type="danger" link @click="handleDeleteData(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 字典数据表单对话框 -->
    <el-dialog
      v-model="dataFormDialogVisible"
      :title="dataFormType === 'add' ? '新增字典数据' : '编辑字典数据'"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="dataFormRef"
        :model="dataFormData"
        :rules="dataFormRules"
        label-width="100px"
      >
        <el-form-item label="字典标签" prop="label">
          <el-input v-model="dataFormData.label" placeholder="请输入字典标签" />
        </el-form-item>
        <el-form-item label="字典值" prop="value">
          <el-input v-model="dataFormData.value" placeholder="请输入字典值" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="dataFormData.sort" :min="0" :max="999" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="dataFormData.status"
            active-value="enabled"
            inactive-value="disabled"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dataFormDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleDataSubmit">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { 
  Plus, 
  Search, 
  Refresh,
  Document,
  Collection,
  CircleCheck,
  CircleClose
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 查询参数
const queryParams = ref({
  name: '',
  type: '',
  status: '',
  pageNum: 1,
  pageSize: 10
})

// 统计数据
const stats = ref({
  total: 25,
  enabled: 20,
  disabled: 5,
  today: 2
})

// 总数
const total = ref(0)

// 字典列表数据
const dictList = ref([
  {
    id: 1,
    name: '用户性别',
    type: 'sys_user_sex',
    status: 'enabled',
    remark: '用户性别列表',
    createTime: '2024-01-01 00:00:00'
  }
])

// 字典数据列表
const dictDataList = ref([
  {
    id: 1,
    label: '男',
    value: '1',
    sort: 1,
    status: 'enabled'
  },
  {
    id: 2,
    label: '女',
    value: '2',
    sort: 2,
    status: 'enabled'
  }
])

// 表单相关
const dialogVisible = ref(false)
const formType = ref('add')
const formRef = ref(null)
const formData = ref({
  name: '',
  type: '',
  status: 'enabled',
  remark: ''
})

// 表单校验规则
const formRules = {
  name: [
    { required: true, message: '请输入字典名称', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请输入字典类型', trigger: 'blur' }
  ]
}

// 字典数据相关
const dataDialogVisible = ref(false)
const currentDict = ref(null)

// 字典数据表单相关
const dataFormDialogVisible = ref(false)
const dataFormType = ref('add')
const dataFormRef = ref(null)
const dataFormData = ref({
  label: '',
  value: '',
  sort: 0,
  status: 'enabled'
})

// 字典数据表单校验规则
const dataFormRules = {
  label: [
    { required: true, message: '请输入字典标签', trigger: 'blur' }
  ],
  value: [
    { required: true, message: '请输入字典值', trigger: 'blur' }
  ]
}

// 搜索
const handleSearch = () => {
  queryParams.value.pageNum = 1
  // TODO: 调用搜索接口
  ElMessage.success('搜索成功')
  console.log('搜索参数:', queryParams.value)
}

// 重置查询
const resetQuery = () => {
  queryParams.value = {
    name: '',
    type: '',
    status: '',
    pageNum: 1,
    pageSize: 10
  }
  handleSearch()
  ElMessage.success('重置成功')
}

// 新增字典
const handleAdd = () => {
  formType.value = 'add'
  formData.value = {
    name: '',
    type: '',
    status: 'enabled',
    remark: ''
  }
  dialogVisible.value = true
}

// 编辑字典
const handleEdit = (row) => {
  formType.value = 'edit'
  formData.value = { ...row }
  dialogVisible.value = true
}

// 删除字典
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定要删除该字典吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 调用删除接口
    ElMessage.success('删除成功')
  })
}

// 查看字典数据
const handleData = (row) => {
  currentDict.value = row
  dataDialogVisible.value = true
}

// 新增字典数据
const handleAddData = () => {
  dataFormType.value = 'add'
  dataFormData.value = {
    label: '',
    value: '',
    sort: 0,
    status: 'enabled'
  }
  dataFormDialogVisible.value = true
}

// 编辑字典数据
const handleEditData = (row) => {
  dataFormType.value = 'edit'
  dataFormData.value = { ...row }
  dataFormDialogVisible.value = true
}

// 删除字典数据
const handleDeleteData = (row) => {
  ElMessageBox.confirm(
    '确定要删除该字典数据吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 调用删除接口
    ElMessage.success('删除成功')
  })
}

// 提交字典表单
const handleSubmit = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      // TODO: 调用保存接口
      ElMessage.success(formType.value === 'add' ? '新增成功' : '修改成功')
      dialogVisible.value = false
    }
  })
}

// 提交字典数据表单
const handleDataSubmit = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      // TODO: 调用保存接口
      ElMessage.success(dataFormType.value === 'add' ? '新增成功' : '修改成功')
      dataFormDialogVisible.value = false
    }
  })
}

// 分页相关
const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  handleSearch()
}
</script>

<style scoped>
.dict-container {
  padding: 20px;
}

.dict-header {
  margin-bottom: 20px;
}

.search-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.search-form {
  flex: 1;
}

:deep(.el-form-item) {
  margin-bottom: 0;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px var(--el-color-primary) inset;
}

:deep(.el-select .el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset !important;
}

:deep(.el-select .el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px var(--el-color-primary) inset !important;
}

.stat-cards {
  margin-bottom: 20px;
}

.stat-card {
  transition: all 0.3s;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-body {
  text-align: center;
  padding: 10px 0;
}

.number {
  font-size: 28px;
  font-weight: bold;
  color: var(--el-color-primary);
  margin-right: 4px;
}

.label {
  font-size: 14px;
  color: #909399;
}

:deep(.el-card__header) {
  padding: 12px 16px;
  border-bottom: 1px solid var(--el-border-color-light);
}

:deep(.el-tag) {
  border-radius: 12px;
}

/* 暗黑模式适配 */
:deep(html.dark) .search-wrapper,
:deep(html.dark) .dict-content {
  background: var(--el-bg-color);
  box-shadow: none;
}

:deep(html.dark) .el-input__wrapper,
:deep(html.dark) .el-select .el-input__wrapper {
  box-shadow: 0 0 0 1px var(--el-border-color) inset !important;
}

:deep(html.dark) .el-input__wrapper:hover,
:deep(html.dark) .el-select .el-input__wrapper:hover {
  box-shadow: 0 0 0 1px var(--el-color-primary) inset !important;
}
</style> 